import 'package:flutter/material.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import 'routes/app_routes.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
      ),
      // Use the route generator
      onGenerateRoute: AppRoutes.generateRoute,
      initialRoute: AppRoutes.home,
      // Set initial route arguments
      onGenerateInitialRoutes: (String initialRouteName) {
        return [
          AppRoutes.generateRoute(
            RouteSettings(
              name: initialRouteName,
              arguments: const MyHomePage(title: 'Flutter Demo Home Page'),
            ),
          ),
        ];
      },
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;
  String? returnedDate;
  List<String>? returnedWeeks;
  String? returnedStartTime;
  String? returnedEndTime;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  void _navigateToDaylightSavingPage() async {
    // Use the navigation method from AppRoutes
    final result = await AppRoutes.navigateToDaylightSaving(
      context,
      initialDate: '2024-08-01',
      initialWeeks: ['周一', '周三', '周五'],
    );

    // Handle the returned data
    if (result != null) {
      setState(() {
        returnedDate = result.selectedDate;
        returnedWeeks = result.selectedWeeks;
        returnedStartTime = result.startTime;
        returnedEndTime = result.endTime;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            const Text('You have pushed the button this many times:'),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
            const SizedBox(height: 20),
            TDButton(
              text: '前往夏令时页面',
              theme: TDButtonTheme.primary,
              onTap: _navigateToDaylightSavingPage,
            ),
            const SizedBox(height: 20),
            if (returnedDate != null) ...[
              const Text('返回的数据:'),
              Text('日期: $returnedDate'),
              Text('星期: ${returnedWeeks?.join(", ")}'),
              Text('开始时间: $returnedStartTime'),
              Text('结束时间: $returnedEndTime'),
            ],
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }
}
